/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.j4sc.submission;

import java.io.StringWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;

import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.SMOutputFactory;
import org.codehaus.staxmate.out.SMOutputDocument;
import org.codehaus.staxmate.out.SMOutputElement;

import com.googlecode.j4sc.PartyInformation;

class SubmissionMetaData {

    private enum Tag {
        REQUEST("request"), 
        ACCOUNT_ID("account-id"), 
        REFERENCE("reference"), 
        APP_NAME("app-name"), 
        INFORMATION("information"), 
        CALLING_PARTY("calling-party"), 
        CALLED_PARTY("called-party"), 
        NAME("name"), 
        IDENTIFIER("identifier");

        private final String tagName;

        private Tag(String tagName) {
            this.tagName = tagName;
        }

        private String getTagName() {
            return tagName;
        }
    }

    static String createXmlMetadata(String accountId, String applicationName, String reference,
            PartyInformation information) throws XMLStreamException {

        SMOutputFactory outf = new SMOutputFactory(XMLOutputFactory.newInstance());
        StringWriter stringWriter = new StringWriter();
        SMOutputDocument doc = outf.createOutputDocument(stringWriter);
        doc.setIndentation(StringUtils.rightPad("\n", 11, ' '), 1, 2);

        SMOutputElement requestElement = doc.addElement(Tag.REQUEST.getTagName());

        addElement(requestElement, Tag.ACCOUNT_ID, accountId);
        addElement(requestElement, Tag.REFERENCE, reference);
        addElement(requestElement, Tag.APP_NAME, applicationName);

        if (information != null && !information.isEmpty()) {
            SMOutputElement informationElement = requestElement.addElement(Tag.INFORMATION.getTagName());
            if (!information.isCallingPartyEmpty()) {
                addParty(informationElement, Tag.CALLING_PARTY, information.getCallingPartyName(), information
                        .getCallingPartyIdentifier());
            }
            if (!information.isCalledPartyEmpty()) {
                addParty(informationElement, Tag.CALLED_PARTY, information.getCalledPartyName(), information
                        .getCalledPartyIdentifier());
            }
        }

        doc.closeRoot();
        return stringWriter.toString();
    }

    private static SMOutputElement addElement(SMOutputElement element, Tag tag, String value) throws XMLStreamException {
        if (value != null) {
            return element.addElementWithCharacters(null, tag.getTagName(), value);
        }
        return null;
    }

    private static void addParty(SMOutputElement parent, Tag tag, String partyName, String partyIdentifier)
            throws XMLStreamException {
        SMOutputElement element = parent.addElement(tag.getTagName());
        addElement(element, Tag.NAME, partyName);
        addElement(element, Tag.IDENTIFIER, partyIdentifier);
    }
}
